package com.example.yuejian.yuejian.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yuejian.yuejian.R;
import com.example.yuejian.yuejian.base.BaseActivity;
import com.example.yuejian.yuejian.base.ModleListener;
import com.example.yuejian.yuejian.bean.SignupTrain;
import com.example.yuejian.yuejian.bean.UsersInfo;
import com.example.yuejian.yuejian.model.TrainModel;
import com.example.yuejian.yuejian.model.UserModel;

import java.text.SimpleDateFormat;
import java.util.Date;

public class TrainSignActivity extends BaseActivity {
    private TextView trainName;
    private  TextView trainType;
    private TextView trainPrice;
    private EditText trainTrueName;
    private TextView phone;
    private ImageButton numAdd;
    private  ImageButton numReduce;
    private EditText buyNum;
    private RadioGroup radioGroup;
    private RadioButton pay_type_zhifabao;
    private RadioButton pay_type_wechat;
    private TextView must_known;
    private  TextView totalPrice;
    private Button btn_pay_ok;

    String train_name;
    String train_money;//单价
    int num=1;//初始数量
    int money;
    int sum;//总数
    double totalmoney;
    String pay_way="支付宝";;
    String createtime;
    String trainid;
    String train_type;

    private ModleListener<UsersInfo> userinfoListener=new ModleListener<UsersInfo>() {
        @Override
        public void onResponse(UsersInfo o) {
            UsersInfo res=o;


            trainTrueName.setText(res.getTurename());
            phone.setText(res.getPhone());
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(getApplication(),msg,Toast.LENGTH_SHORT).show();
        }
    };
    private ModleListener<SignupTrain>signupListener=new ModleListener<SignupTrain>() {
        @Override
        public void onResponse(SignupTrain o) {
             SignupTrain res=o;
            if (0==res.getRetCode())
            {
                Toast.makeText(getApplication(),res.getRetMsg(),Toast.LENGTH_SHORT).show();
            }
            else {
                Toast.makeText(getApplication(),res.getRetMsg(),Toast.LENGTH_SHORT).show();
                finish();
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(getApplication(),"未知错误",Toast.LENGTH_SHORT).show();
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_train_sign);
        initViews();
        initEvents();
        getuserDatas();
        getTraininfo();
        getData();
    }
    private void initViews()
    {
        trainName=(TextView)findViewById(R.id.train_sign_trainname);
        trainType=(TextView)findViewById(R.id.train_sign_type);
        trainPrice=(TextView)findViewById(R.id.train_sign_price);
        trainTrueName=(EditText)findViewById(R.id.train_sign_turename);
        numAdd=(ImageButton)findViewById(R.id.train_imgBtn_up);
        numReduce=(ImageButton)findViewById(R.id.train_imgBtn_down);
        buyNum=(EditText)findViewById(R.id.train_sign_buy_num);
        radioGroup = (RadioGroup) this.findViewById(R.id.pay_type_group);
        pay_type_zhifabao=(RadioButton) findViewById(R.id.train_pay_type_zhifubao);
        pay_type_wechat=(RadioButton) findViewById(R.id.train_pay_type_wechat);
        totalPrice=(TextView)findViewById(R.id.train_sign_all_money);
        btn_pay_ok=(Button)findViewById(R.id.train_sign_pay_btn);
        phone=(TextView)findViewById(R.id.train_sign_phone);

    }
    private void getTraininfo()
    {
        Intent intent=getIntent();
        train_name=intent.getStringExtra("train_name");
        train_money=intent.getStringExtra("money");
        trainid=intent.getStringExtra("train_id");
        train_type=intent.getStringExtra("type");

        trainPrice.setText(train_money+"/人");
        trainType.setText(train_type);
        trainName.setText(train_name);
    }
    private void  getuserDatas()
    {
        UserModel userModel=new UserModel();
        userModel.userInfo(getUser_id(),userinfoListener);
    }
    private  void getData(){

        money=Integer.valueOf(train_money);
        totalPrice.setText("¥"+train_money);

       /* SimpleDateFormat dff = new SimpleDateFormat("yyyy-MM-dd");
        dff.setTimeZone(TimeZone.getTimeZone("GMT+08"));
        String ee = dff.format(new Date());*/

        SimpleDateFormat formatter = new SimpleDateFormat ("yyyy年MM月dd日 HH:mm:ss ");
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        createtime = formatter.format(curDate);
        //Toast.makeText(getApplication(),createtime,Toast.LENGTH_SHORT).show();
        buyNum.setText(num+"");
    }
    private void initEvents()
    {


        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                if(checkedId == pay_type_zhifabao.getId()){
                    pay_way="支付宝";
                    Toast.makeText(TrainSignActivity.this, pay_way, Toast.LENGTH_SHORT).show();
                }else if(checkedId == pay_type_wechat.getId()){
                    pay_way="微信";
                    Toast.makeText(TrainSignActivity.this, pay_way, Toast.LENGTH_SHORT).show();
                }
            }
        });
       // pay_type_zhifabao.setOnClickListener(onClickListener);
       // pay_type_wechat.setOnClickListener(onClickListener);
        numAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String n=buyNum.getText().toString();
                buyNum.setText(String.valueOf(Integer.parseInt(n)+1));
                int sum=   Integer.valueOf(buyNum.getText().toString());
                totalmoney=money*sum;
                totalPrice.setText("¥"+String.valueOf(totalmoney));
            }
        });
        numReduce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String n=buyNum.getText().toString();
                int nn=Integer.parseInt(n)-1;
                buyNum.setText(String.valueOf(nn<1?1:nn));
                sum= Integer.valueOf(buyNum.getText().toString());
                totalmoney=money*sum;
                totalPrice.setText(String.valueOf(totalmoney));
            }
        });
        btn_pay_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                TrainModel model=new TrainModel();
                model.signupTrain(trainid,getUser_id(),sum,trainTrueName.getText().toString(),phone.getText().toString(),
                        totalmoney,pay_way,signupListener);
            }
        });
    }

    View.OnClickListener  onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.train_pay_type_zhifubao:
                    pay_way="支付宝";
                    Toast.makeText(TrainSignActivity.this, pay_way, Toast.LENGTH_SHORT).show();

                    break;
                case R.id.train_pay_type_wechat:
                    pay_way="微信";
                    Toast.makeText(TrainSignActivity.this, pay_way, Toast.LENGTH_SHORT).show();

                    break;
            }

        }
    };

}
